<template>
  <view>
    <!--自定义搜索框 -->
    <view class="search-box">
      <my-search @click="gotoSearch"></my-search>
    </view>
    <!--轮播图 -->
    <swiper class="swiper" :indicator-dots="true" :autoplay="true" :interval="2500" :duration="1000" circular="true">
      <swiper-item v-for="(item, i) in swiperList" :key="item.goods_id">
        <navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`" open-type="navigate">
          <image :src="item.image_src"></image> 
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类导航 -->
    <view class="nav-list">
      <view class="nav-item" v-for="(item,index) in navList" :key="index" @click="navClickHandler(item)">
        <image :src="item.image_src" mode="widthFix"></image>
      </view>
    </view>
    <!-- 楼层 -->
    <view class="floor-list" v-for="(item,index) in floorList" :key="index">
      <!-- 标题 -->
      <view class="floor-title">
        <image :src="item.floor_title.image_src" mode="heightFix"></image>
      </view>
      <!-- 图片区域 -->
      <view class="floor-img-box">
        <!-- img左侧大盒子 -->
        <navigator class="left-img-box" :url="item.product_list[0].navigator_url">
          <image :src="item.product_list[0].image_src" :style="{width:item.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
        </navigator>
        <!-- img右侧4个小盒子 -->
        <view class="right-img-box" >
          <navigator class="right-img-item" v-for="(img,i2) in item.product_list.filter((pro,i3)=>i3>0)" :key="i2" :url="img.navigator_url">
            <image :src="img.image_src" mode="widthFix" :style="{width:img.image_width + 'rpx'}"></image>
          </navigator>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import badgeMix from '@/mixins/tabbar-badge.js'
  export default {
    mixins: [badgeMix],
    data() {
      return {
        swiperList: [], //轮播图数据
        navList: [], // 分类导航数据
        floorList: [], // 楼层数据
      };
    },
    onLoad() {
      this.getSwiperList() // 获取轮播图数据
      this.getNavList() // 获取home分类导航数据
      this.getFloorList() // 获取楼层数据
    },
    methods:{
      // 获取轮播图数据
      async getSwiperList(){
        const {data:res} = await uni.$http.get('/home/swiperdata')
        // console.log(res)
        if(res.meta.status !== 200){ //请求失败时
          return uni.$showMsg()
        }
        this.swiperList = res.message // 数据请求成功
      },
      // 获取home分类导航数据
      async getNavList(){
        const {data:res} = await uni.$http.get('/home/catitems')
        // console.log(res)
        if(res.meta.status !== 200){ //请求失败时
          return uni.$showMsg()
        }
        this.navList = res.message // 数据请求成功
      },
      // 获取楼层数据
      async getFloorList(){
        const {data:res} = await uni.$http.get('/home/floordata')
        // console.log(res)
        if(res.meta.status !== 200){ //请求失败时
          return uni.$showMsg()
        }
        // 使用双重forEach处理product_list中每项图片的页面链接
        res.message.forEach(item => {
          item.product_list.forEach(pro=>{
            pro.navigator_url = `/subpkg/goods_list/goods_list?${pro.navigator_url.split('?')[1]}`
            // console.log(pro.navigator_url);
          })
        })
        this.floorList = res.message // 数据请求成功
      },
      // 点击导航的事件函数
      navClickHandler(item){
        // 判断点击的是哪个 nav
        if(item.name === '分类'){
          uni.switchTab({ // 编程式导航
            url: '/pages/cate/cate'
          })
        }
      },
      // 跳转搜索
      gotoSearch(){
        uni.navigateTo({
          url:'/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
// 吸顶自定义搜索框
.search-box{
  position: sticky;
  top: 0;
  z-index: 999; // 提高层级避免被轮播图覆盖
}
// 轮播图
.swiper{
  height: 330rpx;
  .swiper-item, image{
    width: 100%;
    height: 100%;
  }
}
// home导航
.nav-list{
  width: 100%;
  margin: 15px 0;
  display: flex;
  justify-content: space-around;
  // align-items: center;
    image{
      width: 128rpx;
      height: 140rpx;
    }
}
// 楼层
.floor-list{
  width: 100%;
  .floor-title{
    image{
      height: 60rpx;   
    }
  }
  .floor-img-box{
    display: flex;
    padding: 10rpx;
    .right-img-box{
      display: flex;
      flex-wrap: wrap;
      justify-content: space-around;
    }
  }
}
</style>
